package framework.drawer.swing.gui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.PanelUI;

public class EditorToolsPanelUI extends PanelUI {
    private static final double FULLSIZE_SCALING_FACTOR = 1;
    
    private JPanel toolsPanel;
    private EditorToolsPanel editorToolsPanel;
	
    public EditorToolsPanelUI(EditorToolsPanel editorToolsPanel) {
        this.editorToolsPanel = editorToolsPanel;
    }
    
    //FIXME
    private boolean isFrameworkMenu() {
    	return !editorToolsPanel.getTitle().equals("Menu do iGeomkids");
    }

    @Override
    public void installUI(JComponent c)
    {
        c.removeAll();
        c.setBackground(new Color(238, 238, 238));
        
        List<JButton> buttons = editorToolsPanel.getButtons();
        
        if(isFrameworkMenu()) {
	        for(JButton b : buttons)
	        	b.setUI(new IconButtonUI(FULLSIZE_SCALING_FACTOR));
        }
        
        c.setLayout(new FlowLayout(FlowLayout.CENTER));
        c.add(getToolsPanel());
    }


    private JPanel getToolsPanel()
    {
        if (this.toolsPanel == null) {
            this.toolsPanel = new JPanel();
            this.toolsPanel.setOpaque(false);
            this.toolsPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
            
            List<JButton> buttons = editorToolsPanel.getButtons();
            for(JButton b : buttons)
            	toolsPanel.add(b);

            GridBagLayout layout = new GridBagLayout();
            this.toolsPanel.setLayout(layout);

//            int i = 0;
//            for(JButton b : buttons) {
//            	GridBagConstraints c = new GridBagConstraints();
//                c.anchor = GridBagConstraints.CENTER;
//                c.insets = new Insets(0, 0, 5, 15);
//                c.gridx = i;
//                c.gridy = 0;
//                layout.setConstraints(b, c);
//                i++;
//            }
            
            if(isFrameworkMenu()) {
            	for(int i = 0; i < 2; i++) {
            		for (int j = 0; (j < 5) && (i*5 + j < buttons.size()); j++) {
            			GridBagConstraints c = new GridBagConstraints();
        	            c.anchor = GridBagConstraints.CENTER;
        	            c.insets = new Insets(0, 0, 5, 15);
        	            c.gridx = j;
        	            c.gridy = i;
        	            layout.setConstraints(buttons.get(i*5 + j), c);
            		}
            	}
            }
            else {
            	for(int i = 0; i < buttons.size()/2 + 1; i++) {
            		for (int j = 0; (j < 2) && (i*2 + j < buttons.size()); j++) {
            			GridBagConstraints c = new GridBagConstraints();
        	            c.anchor = GridBagConstraints.CENTER;
        	            c.insets = new Insets(0, 0, 5, 15);
        	            c.gridx = j;
        	            c.gridy = i;
        	            layout.setConstraints(buttons.get(i*2 + j), c);
            		}
            	}
            }
        }
        
        return this.toolsPanel;
    }

}
